const { getEssay,createEssay,destroyEssay,updateEssay,changeEssayStatus } = require('../service/essay'),
      { updateToQiniu , returnInfo } = require('../lib/utils'),
      { nanoid } = require('nanoid'),
      { IMAGE_UPLOAD,API } =require('../config/err_config'),
      { qiniu } = require('../config/config');
const path = require('path')
const fs = require('fs');
class Essay {
  async getEssay(ctx, next) {
    const {tid,offset,limit = 2} = ctx.request.body
    let returnMess = await getEssay(tid,Number(offset)-1,Number(limit));
    ctx.body = returnMess ? returnInfo(API.GET_ESSAY_SUCCESS,returnMess)
                          : returnInfo(API.GET_ESSAY_ERROR)
  }
  async createEssay(ctx, next) {
    let essayData = ctx.request.body;
    let returnMess = await createEssay(essayData);
    ctx.body = returnMess ? returnInfo(API.CREATE_ESSAY_SUCCESS)
                          : returnInfo(API.CREATE_ESSAY_ERROR)
    
  }
  async destroyEssay(ctx){
    let {id} = ctx.request.body;
    let returnMess = await destroyEssay(id);
    ctx.body = returnMess ? returnInfo(API.DESTROY_ESSAY_SUCCESS)
                          : returnInfo(API.DESTROY_ESSAY_ERROR)
  }
  async updateEssay(ctx){
    let essayData = ctx.request.body;
    let returnMess = await updateEssay(essayData);
    ctx.body = returnMess ? returnInfo(API.UPDATE_ESSAY_SUCCESS)
                          : returnInfo(API.UPDATE_ESSAY_ERROR)
  }
  async changeStatusEssay(ctx){
    let {id} = ctx.request.body;
    let returnMess = await destroyEssay(id);
    ctx.body = returnMess ? returnInfo(API.CHANGE_STATUS_ESSAY_SUCCESS)
                          : returnInfo(API.CHANGE_STATUS_ESSAY_ERROR)
  }
  uploadImage(ctx, next) {
    //这是单文件上传
    // const file = ctx.request.files.qqq;
    // console.log(file.path);
    // let extname = path.extname(file.name);
    // let reader = fs.createReadStream(file.path);
    // let writer = fs.createWriteStream(
    //   `static/${Math.random()
    //     .toString(36)
    //     .substr(2)}${extname}`
    //     );
    //     reader.pipe(writer);
    //这是多文件上传    
    // const files = Object.entries(ctx.request.files);

    // files.map((item,index)=>{
    //   console.log(item[0],item[1]);
    //   let extname = path.extname(item[1].name);
    //   let reader = fs.createReadStream(item[1].path);
    //   let writer = fs.createWriteStream(
    //     `static/${Math.random()
    //       .toString(36)
    //       .substr(2)}${extname}`

    //   );
    //   reader.pipe(writer);
    //     })
    const files = Object.entries(ctx.request.files);
      let imgUrlAr = [];
      files.map((item)=>{
        let extname = path.extname(item[1].name);
        let filePath = item[1].path;
        let fileName = `essayphoto${nanoid()}${extname}`;
        updateToQiniu(filePath,fileName);     
        imgUrlAr.push(`${qiniu.bucket.blogsimg.domain}/${fileName}` )
      })
      
      if(imgUrlAr.length <= 0){
        ctx.body = IMAGE_UPLOAD.UPLOAD_ERROR;
      }
      ctx.body =
      {
        // errno 即错误代码，0 表示没有错误。
        //       如果有错误，errno != 0，可通过下文中的监听函数 fail 拿到该错误码进行自定义处理
        "errno": 0,

        // data 是一个数组，返回图片的线上地址
        "data": imgUrlAr
      }
   
  }
}
module.exports = new Essay();